<?php
define('DS','/');
define('LIBRARY','app');
define('MODULES_DIR',   LIBRARY.DS.'code');
define('EXT_LIB',       LIBRARY.DS.'lib');
define('DESIGN_DIR',    LIBRARY.DS.'design'.DS);
define('MODULE_PATH',   MODULES_DIR.DS.'%s'.DS.'%s');
define('MODULE_CONFIG_FILE','etc'.DS.'config.xml');
define('MODULE_CONFIGS',LIBRARY.DS.'etc'.DS.'modules'.DS.'*.xml');
define('MODULE_NAMESPACES', 'core,local');
define('DESIGN_DEFAULT_NAMESPACE','default');

require_once 'app/object.php';
require_once 'app/core.php';

final class AMS {
    static private $registry = null;
    static private $router = null;
    static private $request = null;
    static private $design = null;
    static private $started = false;
    static private $singletons = array();
    static private $home;
    //static private $app = null;
    /**
     * AMS Startup
     */
    public static function app($debug=false) {
        if (self::$started) {
        } else {
            if ($debug) ini_set('display_errors', 1);
            $moduleConfiguration = new AMSModuleConfiguration();
            $config = $moduleConfiguration->init();
            self::$home = preg_replace('</'.LIBRARY.'$>','',dirname(__FILE__)).DS;
            
            set_include_path(get_include_path() . PATH_SEPARATOR . self::$home.EXT_LIB);
            
            self::$registry = new Registry($config);

            spl_autoload_register('autoload');

            self::$request = new AMS_Core_Controller_Request($config);
            self::$design = new AMS_Core_Model_Design();
            self::$router = new AMS_Core_Controller_Router();
            self::$design->setDesign('DESIGN_DEFAULT_NAMESPACE')->setStage(self::$request->getStage());
            self::$router->Route();
            self::$started = true;
        }
    }

    public static function getDesign() {
        return self::$design;
    }
    
    public static function getBaseDir() {
        return self::$home;
    }
    
    public static function getRequest() {
        return self::$request;
    }
    /**
     * Get model from factory
     * @param string $modelName
     * @return AMS_Core_Model_Abstract
     */
    public static function getModel($modelName) {
        $modules = self::$registry->getModules();
        list($alias,$model) = explode('/',$modelName);
        if (isset($modules['models'][$alias])) {
            $module = $modules['models'][$alias];
            $modelName = $module['module'].'_Model_'.ucname($model);
            return new $modelName();
        }
    }
    
    /**
     * Save value as key in registry
     * @param string $key
     * @param mixed $value
     */
    public static function register($key, $value) {
        $this->registry->setData($key,$value);
    }
    
    /**
     * Read value from registry (or return registry instance if no params used)
     * @param string $key
     * @return mixed
     */
    public static function registry($key='') {
        if ($key!='') return self::$registry->getData($key);
        else return self::$registry;
    }
    
    public static function getSingleton($name) {
        if (!isset(self::$singletons[$name]) && $model = self::getModel($name)) self::$singletons[$name] = $model;
        if (isset(self::$singletons[$name])) return self::$singletons[$name];
    }
}

function ucclass($class) {
    return str_replace(' ', DS, ucwords(str_replace('_', ' ', $class)));
}
function ucname($class) {
    return preg_replace('/_([a-z])/e','strtoupper(\'_\\1\')',ucfirst($class));
}

function autoload($class) {
    if (strpos($class,'Zend_')!==0) {
        $namespace = AMS::registry()->findModuleNamespace($class);
        if (strpos($class,'Controller')==(strlen($class)-strlen('Controller'))) {
            $classFile = ucclass($class);
            $classFile = substr($classFile,0,strrpos($classFile,DS)) . DS . 'controllers' . strrchr($classFile,DS);
            $classFile = MODULES_DIR.DS.$namespace.DS.$classFile.'.php';
        }
        else $classFile = MODULES_DIR.DS.$namespace.DS.ucclass($class).'.php';
    } else {
        $classFile = AMS::getBaseDir().EXT_LIB.DS.(str_replace('_',DS,$class)).'.php';
    }
    return include $classFile;
}
?>
